package com.ruyuan.commodity.consumer.core.consumer.goodssearch;

import cn.hutool.json.JSONUtil;
import com.ruyuan.commodity.consumer.api.GoodsFeignClient;
import com.ruyuan.commodity.consumer.api.GoodsIndexFeignClient;
import com.ruyuan.commodity.consumer.core.event.GoodsChangeEvent;
import com.ruyuan.commodity.domain.IndexRequest;
import com.ruyuan.commodity.open.api.model.base.message.GoodsChangeMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 商品信息变更es商品文档的消费者
 */
@Service
public class GoodsChangeIndexConsumer implements GoodsChangeEvent {

    private static final Logger LOG = LoggerFactory.getLogger(GoodsChangeIndexConsumer.class);

    /**
     * 商品的feign client组件
     */
    @Autowired
    private GoodsFeignClient goodsFeignClient;
    /**
     * 商品索引的feign client组件
     */
    @Autowired
    private GoodsIndexFeignClient goodsIndexFeignClient;

    @Override
    public void goodsChange(GoodsChangeMsg goodsChangeMsg) throws IOException {
        LOG.info("进入 商品信息变更 变更es商品文档的消费者逻辑 接收到的消息为 [{}]", JSONUtil.toJsonStr(goodsChangeMsg));
        Long[] goodsIds = goodsChangeMsg.getGoodsIds();
        // 获取商品变动操作类型
        int operationType = goodsChangeMsg.getOperationType();
        List<Map<String, Object>> list = goodsFeignClient.getGoodsAndParams(goodsIds);

        // 添加
        if (GoodsChangeMsg.ADD_OPERATION == operationType) {

            if (list != null && list.size() > 0) {
                Boolean flag = goodsIndexFeignClient.addIndex(buildGoodsIndexRequest(list.get(0)));
                LOG.info("添加商品文档的结果为 [{}]", flag);
            }

        } else if (GoodsChangeMsg.UPDATE_OPERATION == operationType
                || GoodsChangeMsg.INRECYCLE_OPERATION == operationType
                || GoodsChangeMsg.REVERT_OPERATION == operationType
                || GoodsChangeMsg.GOODS_VERIFY_SUCCESS == operationType
                || GoodsChangeMsg.GOODS_VERIFY_FAIL == operationType) {
            // 修改(修改，还原，审核)
            if (list != null) {
                for (Map<String, Object> map : list) {
                    Boolean flag = goodsIndexFeignClient.updateIndex(buildGoodsIndexRequest(map));
                    LOG.info("更新商品文档的结果为 [{}]", flag);
                }
            }

        } else if (GoodsChangeMsg.DEL_OPERATION == operationType || GoodsChangeMsg.UNDER_OPERATION == operationType) {
            // 删除
            if (list != null) {
                for (Map<String, Object> map : list) {
                    Boolean flag = goodsIndexFeignClient.deleteIndex(buildGoodsIndexRequest(map));
                    LOG.info("删除商品文档的结果为 [{}]", flag);
                }
            }
        }
    }

    /**
     * 构建变更商品索引的请求
     * @param goodsInfoAndGoodsParamMap 商品基础信息 以及 商品属性信息的map集合
     * @return 索引变更的请求对象
     */
    private IndexRequest buildGoodsIndexRequest(Map<String, Object> goodsInfoAndGoodsParamMap) {
        IndexRequest indexRequest = new IndexRequest();
        indexRequest.setGoodsInfoMap(goodsInfoAndGoodsParamMap);
        return indexRequest;
    }

}
